package com.pactera.asmp.server.controller.projectmanager;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.pactera.asmp.server.common.constants.Constants;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.exception.CustomException;
import com.pactera.asmp.server.entity.DelProjectUserDTO;
import com.pactera.asmp.server.entity.ProjectInfoSearchDTO;
import com.pactera.asmp.server.entity.ProjectModuleBindDto;
import com.pactera.asmp.server.entity.ProjectModuleDTO2;
import com.pactera.asmp.server.entity.ProjectProductionConfig;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.pojo.ProjectInfo;
import com.pactera.asmp.server.pojo.ProjectVersionInfo;
import com.pactera.asmp.server.pojo.UserProject;
import com.pactera.asmp.server.service.ProjectManagerService;
import com.pactera.asmp.server.utils.PageDataResult;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: asmpserver
 * @Package: com.pactera.asmp.server.controller.projectmanager
 * @ClassName: ProjectManagerController
 * @Author: lvzhiqiang
 * @Description: 项目管理控制类
 * @Date: 2019/8/9 18:05
 * @Version: 1.0
 */
@RestController
@RequestMapping("/projectManager")
public class ProjectManagerController {
    private static final Logger logger = LoggerFactory
            .getLogger(ProjectManagerController.class);

    @Autowired
    private ProjectManagerService mProjectManagerService;

    /**
     * @api {post} /projectManager/getProjectList getProjectList
     * @apiDescription 查找项目列表
     * @apiName getProjectList
     * @apiGroup Project manage
     * @apiParamExample {json} Request-Example:
     *{
     * page: 1
     * limit: 10
     * }
     * @apiSuccessExample Success-Response:
     * {
     *     "totals":6,
     *     "list":[
     *         {
     *             "id":6,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"GEM-K257-GEM升级项目",
     *             "projectDescription":"GEM-K257-GEM升级项目",
     *             "projectStatus":1,
     *             "createTime":"2020-04-28 17:01:20.0",
     *             "completionTime":"2020-04-28 17:01:22.0",
     *             "updateTime":"2020-04-28 17:01:24.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         },
     *         {
     *             "id":5,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"K259泛亚项目",
     *             "projectDescription":"K259泛亚项目",
     *             "projectStatus":1,
     *             "createTime":"2020-04-28 17:00:09.0",
     *             "completionTime":"2020-04-28 17:00:12.0",
     *             "updateTime":"2020-04-28 17:00:15.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         },
     *         {
     *             "id":4,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"GEM",
     *             "projectDescription":"GEM",
     *             "projectStatus":1,
     *             "createTime":"2020-04-26 14:44:37.0",
     *             "completionTime":"2020-04-26 14:44:41.0",
     *             "updateTime":"2020-04-29 14:44:48.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         },
     *         {
     *             "id":3,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"泛亚k257",
     *             "projectDescription":"k257升级项目",
     *             "projectStatus":1,
     *             "createTime":"2019-08-09 13:27:23.0",
     *             "completionTime":"2020-07-28 12:27:23.0",
     *             "updateTime":"2019-12-28 12:27:23.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         },
     *         {
     *             "id":2,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"泛亚k358",
     *             "projectDescription":"K358别克商务车升级项目",
     *             "projectStatus":1,
     *             "createTime":"2019-08-09 12:27:23.0",
     *             "completionTime":"2020-07-28 12:27:23.0",
     *             "updateTime":"2019-12-28 12:27:23.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         },
     *         {
     *             "id":1,
     *             "page":null,
     *             "limit":null,
     *             "projectName":"泛亚k226",
     *             "projectDescription":"k226升级项目",
     *             "projectStatus":1,
     *             "createTime":"2019-07-28 12:27:23.0",
     *             "completionTime":"2020-07-28 12:27:23.0",
     *             "updateTime":"2019-12-28 12:27:23.0",
     *             "createUserId":null,
     *             "updateUserId":null
     *         }
     *     ],
     *     "code":200
     * }
     **/
    @PostMapping("/getProjectList")
    @ResponseBody
    public ResponseResult getProjectList(@RequestBody ProjectInfoSearchDTO projectInfoSearchDTO) {
        Integer page = projectInfoSearchDTO.getPage();
        Integer limit = projectInfoSearchDTO.getLimit();
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        PageDataResult pdr = new PageDataResult();
        try {
            JSONObject jsonObject = new JSONObject();
            if (null == page || page.intValue() == 0) {
                page = 1;
            }
            if (null == limit || limit.intValue() == 0) {
                limit = 10;
            }
            if(page.intValue() < 0 || limit.intValue() < 0) {
                jsonObject.put("totals", 0);
                jsonObject.put("list", Lists.newArrayList());
                responseResult.setData(jsonObject);
                return responseResult;
            }
            pdr = mProjectManagerService.getProjectList(projectInfoSearchDTO, page, limit);
            jsonObject.put("totals", pdr.getTotals());
            jsonObject.put("list", pdr.getList());
            responseResult.setData(jsonObject);
        } catch (CustomException e) {
            responseResult = new ResponseResult(e.getResultCode());
            logger.error("ProjectManager列表查询异常！", e);
        } catch (Exception e) {
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            logger.error("ProjectManager列表查询异常！", e);
        }
        return responseResult;
    }

    /**
     * @api {post} /projectManager/delProjects delProjects
     * @apiDescription 批量删除项目
     * @apiName delProjects
     * @apiGroup Project manage
     * @apiParamExample {json} Request-Example:
     *{
     * datas: [
     * {
     *     "id":3,
     *     "page":null,
     *     "limit":null,
     *     "projectName":"泛亚k257",
     *     "projectDescription":"k257升级项目",
     *     "projectStatus":1,
     *     "createTime":"2019-08-09 13:27:23.0",
     *     "completionTime":"2020-07-28 12:27:23.0",
     *     "updateTime":"2019-12-28 12:27:23.0",
     *     "createUserId":null,
     *     "updateUserId":null
     * }
     * @apiSuccessExample Success-Response:
     * {
     * "删除成功"
     * }
     **/
    @PostMapping(value = "/delProjects")
    @ResponseBody
    public ResponseResult delProjects(@RequestBody JSONObject jsonParam) {
        ResponseResult responseResult;
        List<Integer> listData = (List<Integer>) jsonParam.get("projects");
        try {
            if (!listData.isEmpty()) {
                String msg = mProjectManagerService.delProjects(listData);
                if(ResultCode.SUCCESS.message().equals(msg)){
                    responseResult = new ResponseResult(ResultCode.SUCCESS);
                }else {
                    responseResult = new ResponseResult(ResultCode.FAIL);
                }
            } else {
                responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
            }
        } catch (Exception e) {
            logger.error("删除项目异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            responseResult.setMessage("删除项目异常！");
        }
        return responseResult;
    }

    @GetMapping("/projectList")
    public String projectList(){
        return "/project/projectManager";
    }

    @GetMapping("/projectEchartLine")
    public String projectEchartLine(){
        return "/project/projectEchartLine";
    }

    /**
     * 查找项目名称
     * @return
     */
    /**
     * @api {post} /projectManager/getProjectName getProjectName
     * @apiDescription 查找项目名称
     * @apiName getProjectName
     * @apiGroup Project manage
     * @apiSuccessExample Success-Response:
     * {
     * [
     *     {
     *         "id":4,
     *         "projectName":"GEM",
     *         "projectDescription":"GEM",
     *         "projectStatus":1,
     *         "createTime":1587883477000,
     *         "completionTime":1587883481000,
     *         "updateTime":1588142688000,
     *         "createUserId":1,
     *         "updateUserId":1
     *     },
     *     {
     *         "id":6,
     *         "projectName":"GEM-K257-GEM升级项目",
     *         "projectDescription":"GEM-K257-GEM升级项目",
     *         "projectStatus":1,
     *         "createTime":1588064480000,
     *         "completionTime":1588064482000,
     *         "updateTime":1588064484000,
     *         "createUserId":1,
     *         "updateUserId":1
     *     },
     *     {
     *         "id":5,
     *         "projectName":"K259泛亚项目",
     *         "projectDescription":"K259泛亚项目",
     *         "projectStatus":1,
     *         "createTime":1588064409000,
     *         "completionTime":1588064412000,
     *         "updateTime":1588064415000,
     *         "createUserId":1,
     *         "updateUserId":1
     *     },
     *     {
     *         "id":1,
     *         "projectName":"泛亚k226",
     *         "projectDescription":"k226升级项目",
     *         "projectStatus":1,
     *         "createTime":1564288043000,
     *         "completionTime":1595910443000,
     *         "updateTime":1577507243000,
     *         "createUserId":1,
     *         "updateUserId":1
     *     },
     *     {
     *         "id":2,
     *         "projectName":"泛亚k358",
     *         "projectDescription":"K358别克商务车升级项目",
     *         "projectStatus":1,
     *         "createTime":1565324843000,
     *         "completionTime":1595910443000,
     *         "updateTime":1577507243000,
     *         "createUserId":1,
     *         "updateUserId":1
     *     }
     * ]
     * }
     **/
//    @RequiresPermissions(value = {PermissionCode.TEST_TASK, PermissionCode.TEST_OVERVIEW}, logical = Logical.OR)
    @GetMapping("/getProjectName")
    @ResponseBody
    public ResponseResult getProjectName() {
        logger.debug("查找项目名称!");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        JSONObject jsonObject = new JSONObject();
        List<ProjectInfo> projectName = null;
        try {
            projectName = this.mProjectManagerService.getProjectName();
            jsonObject.put("list", projectName);
            responseResult.setData(jsonObject);
        } catch (CustomException e) {
            logger.error("查找异常！", e);
            responseResult = new ResponseResult(e.getResultCode());
        } catch (Exception e) {
            logger.error("查找异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
        }
        return responseResult;
    }


    /**
     * 查找某项目最近5个版本
     * @return
     */
    /**
     * @api {post} /projectManager/getRecentVersion getRecentVersion
     * @apiDescription 查找某项目最近5个版本
     * @apiName getRecentVersion
     * @apiGroup Project manage
     * @apiParamExample {json} Request-Example:
     *{
     * projectName: GEM
     * }
     * @apiSuccessExample Success-Response:
     * {
     * [
     *     {
     *         "deviceVersionId":null,
     *         "deviceProjectName":null,
     *         "deviceVersionName":"Delphi:24.36.22.32",
     *         "createTime":null
     *     },
     *     {
     *         "deviceVersionId":null,
     *         "deviceProjectName":null,
     *         "deviceVersionName":"Delphi:23.36.22.32",
     *         "createTime":null
     *     },
     *     {
     *         "deviceVersionId":null,
     *         "deviceProjectName":null,
     *         "deviceVersionName":"Delphi:21.36.22.32",
     *         "createTime":null
     *     }
     * ]
     * }
     **/
    @GetMapping("/getRecentVersion")
    @ResponseBody
    public ResponseResult getRecentVersion(@RequestParam String projectName) {
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        try {
            List<ProjectVersionInfo> pv = mProjectManagerService.getRecentVersion(projectName);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", pv);
            responseResult.setData(jsonObject);
        }catch (Exception e){
            logger.error(e.getMessage());
        }

        return responseResult;
    }

    /**
     * 根据项目名称查看该项目所有版本信息
     * @param projectName
     * @return
     */
    @GetMapping("/getAllVersion")
    @ResponseBody
    public ResponseResult getAllVersion(@RequestParam String projectName) {
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        try {
            List<ProjectVersionInfo> pv = mProjectManagerService.getAllVersion(projectName);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", pv);
            responseResult.setData(jsonObject);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return responseResult;
    }

    /**
     * @api {POST} /projectManager/add add
     * @apiVersion 1.0.0
     * @apiGroup ProjectManagerController
     * @apiName add
     * @apiDescription 新增项目，及其绑定的模块、用户、设备、dbc文件
     * @apiParam (请求参数) {Array} dbcFiles dbc文件数组
     * @apiParam (请求参数) {Array} userIds 用户ID数组
     * @apiParam (请求参数) {Array} deviceIds 设备ID数组
     * @apiParam (请求参数) {Array} moduleNames 模块名数组
     * @apiParamExample 请求参数示例
     * dbcFiles=&deviceIds=1,2&userIds=1,2&moduleNames=a,b
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":4227,"data":{},"message":"WSJH"}
     */
    @PostMapping("/add")
    public ResponseResult add(MultipartFile[] dbcFiles, String userProjects, Integer[] deviceIds, Integer[] masterComputerIds,
                              String modules, HttpServletRequest request) {
        List<ProjectModuleDTO2> projectModuleDTOs = null;
        if(StringUtils.isNotEmpty(modules)) {
            projectModuleDTOs = JSONObject.parseArray(modules, ProjectModuleDTO2.class);
            // 判断输入项目名称是否重复
            Set<String> names = projectModuleDTOs.stream().map(x -> x.getName()).collect(Collectors.toSet());
            if(names.size() != projectModuleDTOs.size()) {
                throw new CustomException(ResultCode.PARAM_DUPLICATION);
            }
            for(ProjectModuleDTO2 projectModuleDTO2 : projectModuleDTOs) {
                if(!projectModuleDTO2.getName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
                    return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
                }
            }
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        mProjectManagerService.add(dbcFiles,  StringUtils.isNotEmpty(userProjects) ? JSONObject.parseArray(userProjects, UserProject.class) : null, deviceIds, masterComputerIds, projectModuleDTOs, request);
        return responseResult;
    }

    /**
     * @api {GET} /projectManager/detail detail
     * @apiVersion 1.0.0
     * @apiGroup ProjectManagerController
     * @apiName detail
     * @apiDescription 获取项目详情，及其绑定的模块和用户
     * @apiParam (请求参数) {Number} projectId 项目ID
     * @apiParamExample 请求参数示例
     * projectId=1590
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":5789,"data":{},"message":"at3Gmb21Xr"}
     */
    @GetMapping("/detail")
    public ResponseResult detail(Integer projectId) {
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        responseResult.setData(mProjectManagerService.detail(projectId));
        return responseResult;
    }

    /**
     * @api {POST} /projectManager/update update
     * @apiVersion 1.0.0
     * @apiGroup ProjectManagerController
     * @apiName update
     * @apiDescription 更新项目及其绑定的模块、用户、dbc
     * @apiParam (请求参数) {Array} dbcFiles dbc文件数组 （增量数据）
     * @apiParam (请求参数) {Array} dbcDeleteIds 删除了的dbc文件ID数组 （增量数据）
     * @apiParam (请求参数) {Array} userIds 用户数组 (全量数据)
     * @apiParam (请求参数) {Array} moduleNames 模块名数组(全量数据)
     * @apiParam (请求参数) {String} projectId 项目ID
     * @apiParam (请求参数) {String} projectName 项目名
     * @apiParam (请求参数) {String} projectDescription 项目描述
     * @apiParam (请求参数) {String} projectStatus 项目状态
     * @apiParamExample 请求参数示例
     * dbcDeleteIds=9332,9333&dbcFiles=&userIds=3646,3647&moduleNames=a,b
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":4243,"data":{},"message":"7"}
     */
    @PostMapping("/update")
    public ResponseResult update(MultipartFile[] dbcFiles, Long[] dbcDeleteIds, String userProjects,
                                 String modules, HttpServletRequest request) {
        List<ProjectModuleDTO2> projectModuleDTOs = null;
        if(StringUtils.isNotEmpty(modules)) {
            projectModuleDTOs = JSONObject.parseArray(modules, ProjectModuleDTO2.class);
            // 判断输入项目名称是否重复
            Set<String> names = projectModuleDTOs.stream().map(x -> x.getName()).collect(Collectors.toSet());
            if(names.size() != projectModuleDTOs.size()) {
                throw new CustomException(ResultCode.PARAM_DUPLICATION);
            }
            for(ProjectModuleDTO2 projectModuleDTO2 : projectModuleDTOs) {
                if(!projectModuleDTO2.getName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
                    return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
                }
            }
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        mProjectManagerService.update(dbcFiles, dbcDeleteIds, StringUtils.isNotEmpty(userProjects) ? JSONObject.parseArray(userProjects, UserProject.class) : null, projectModuleDTOs, request);
        return responseResult;
    }


    /**
     * @api {POST} /projectManager/batchUpdateProjectModule batchUpdateProjectModule
     * @apiVersion 1.0.0
     * @apiGroup ProjectManagerController
     * @apiName batchUpdateProjectModule
     * @apiDescription 批量更新项目模块
     * @apiParam (请求体) {Number} projectId 项目ID
     * @apiParam (请求体) {Array} moduleNames 模块名字符串数组，只需传入当前有效的
     * @apiParamExample 请求体示例
     * {"moduleNames":["g"],"projectId":5230}
     * @apiHeader {String} Authorization Bearer xxx
     * @apiHeaderExample {json} Request-Example:
     * { "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1dWlkIjoiNTAiLCJjaGFubmVsSWQiOjUsInJvbGVJZCI6IjIiLCJjcmVhdGVUaW1lIjoxNjE1MzU5MDgxNDY1LCJsb2NhbGhvc3QiOiJCQUIxOTAwMjE1Iiwic3ViIjoiYWRtaW4iLCJpc3MiOiIyZDBhNDI2MTY5MjYwYzA4NmIxNWU4MzlhYmEyMGUwOSIsImlhdCI6MTYxNTM1OTA4MSwiYXVkIjoicmVzdGFwaXVzZXIiLCJleHAiOjE2MTc5NTEwODEsIm5iZiI6MTYxNTM1OTA4MX0.BpUVtIE4kWyWKD8-b7lAv1611rv5N1U1q0VO4ijD0P0" }
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Object} data
     * @apiSuccessExample 响应结果示例
     * {"code":9996,"data":{},"message":"Uq4Ez"}
     */
    @PostMapping("/batchUpdateProjectModule")
    public ResponseResult batchUpdateProjectModule(@RequestBody ProjectModuleBindDto projectModuleBindDto) {
        logger.info("/projectManager/batchUpdateProjectModule projectModuleBindDto: {}", projectModuleBindDto);
        if (null == projectModuleBindDto) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        mProjectManagerService.updateProjectModule(projectModuleBindDto);
        return responseResult;
    }


    /**
     * @api {GET} /projectManager/unbindProject getUnbindProject
     * @apiVersion 1.0.0
     * @apiGroup ProjectManagerController
     * @apiName getUnbindProject
     * @apiDescription 获取当前指定设备尚未绑定的项目列表
     * @apiParam (请求参数) {Number} deviceId 设备ID
     * @apiParamExample 请求参数示例
     * deviceId=2722
     * @apiSuccess (响应结果) {Number} code
     * @apiSuccess (响应结果) {String} message
     * @apiSuccess (响应结果) {Array} data
     * @apiSuccess (响应结果) {Number} data.id 项目ID
     * @apiSuccess (响应结果) {String} data.projectName 项目名称
     * @apiSuccess (响应结果) {String} data.projectDescription 项目描述
     * @apiSuccess (响应结果) {Number} data.projectStatus 项目状态
     * @apiSuccess (响应结果) {Number} data.createTime  创建时间
     * @apiSuccess (响应结果) {Number} data.completionTime  完成时间
     * @apiSuccess (响应结果) {Number} data.updateTime  修改时间
     * @apiSuccess (响应结果) {Number} data.createUserId    创建人员ID
     * @apiSuccess (响应结果) {Number} data.updateUserId    修改人员ID
     * @apiSuccessExample 响应结果示例
     * {"code":4735,"data":[{"projectStatus":6215,"completionTime":58385793661,"createUserId":9775,"createTime":2825284848417,"updateUserId":7968,"updateUserName":"GGo4QFWKxK","projectDescription":"q4N","updateTime":3881666643957,"createUserName":"uyoaTi","id":4381,"projectName":"QSWSSKOM"}],"message":"85QCM1M6"}
     */
    @GetMapping("/unbindProject")
    public ResponseResult<List<ProjectInfo>> getUnbindProject(@RequestParam("deviceId") Integer deviceId) {
        logger.info("/projectManager/unbindProject getUnbindProject: {}", deviceId);
        return ResponseResult.defaultSuccess(mProjectManagerService.getProjectUnbindByDeviceId(deviceId));
    }

    /**
     * 删除
     * 
     * @param userIds
     * @param projectId
     * @return
     */
    @DeleteMapping("/delProjectUser")
    public ResponseResult delProjectUser(@RequestBody DelProjectUserDTO delProjectUserDTO) {
        logger.info("/projectManager/delProjectUser delProjectUser: {}  {}", delProjectUserDTO.getUserIds(), delProjectUserDTO.getProjectId());
        if(null == delProjectUserDTO.getUserIds() || delProjectUserDTO.getUserIds().length == 0 || null == delProjectUserDTO.getProjectId()) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK);
        }
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
        int count = mProjectManagerService.delProjectUser(delProjectUserDTO.getUserIds(), delProjectUserDTO.getProjectId());
        if(count < 1) {
            responseResult = new ResponseResult(ResultCode.FAIL);
        }
        return responseResult;
    }

    /**
     * 导出项目信息
     * 
     * @param projectInfoSearchDTO
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void exportProjectManager(@RequestBody ProjectInfoSearchDTO projectInfoSearchDTO, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        logger.info("/projectManager/export {}" + JSONObject.toJSONString(projectInfoSearchDTO));
        mProjectManagerService.export(projectInfoSearchDTO, response);
    }

    /**
     * 获取项目流水线配置信息
     * 
     * @param projectId
     */
    @GetMapping(value = "/getProductionConfig")
    public ResponseResult<ProjectProductionConfig> getProductionConfig(@RequestParam("projectId") Integer projectId) {
        logger.info("/projectManager/getProductionConfig {}" + projectId);
        return ResponseResult.defaultSuccess(mProjectManagerService.getProductionConfig(projectId)) ;
    }

}
